package com.jrtc.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.base.Strings;
import com.jrtc.base.config.dto.ResultDTO;
import com.jrtc.base.config.dto.ResultDTOBuilder;
import com.jrtc.base.entity.bo.SysDict;
import com.jrtc.service.SysDictService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 字典表(SysDict)表控制层
 *
 * @author makejava
 * @since 2022-09-06 16:33:58
 */
@RestController
@RequestMapping("sysDict")
public class SysDictController {
    /**
     * 服务对象
     */
    @Resource
    private SysDictService sysDictService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public SysDict selectOne(Integer id) {
        return this.sysDictService.queryById(id);
    }
    
//    @RequestMapping("getSysDictList")
//    public ResultDTO getSysDictList(@RequestBody SysDict sysDict)
//    {
//        IPage<SysDict> ipage = sysDictService.querySysDictList(sysDict);
//        return ResultDTOBuilder.success(ipage);
//    }

    @RequestMapping("insertSysDict")
    public ResultDTO insertSysDict(@RequestBody SysDict sysDict)
    {
        SysDict tmpSysDict = sysDictService.insert(sysDict);
        if (tmpSysDict != null)
        {
           return ResultDTOBuilder.success("添加成功！");
        }
        return ResultDTOBuilder.failureMsg("添加失败！");
    }

    @RequestMapping("getSysDictById")
    public ResultDTO getSysDictById(@RequestBody Map<String, String> map)
    {
        String sysDictId = map.get("sysDictId");
        SysDict sysDict = sysDictService.queryById(Integer.parseInt(sysDictId));
        if (Strings.isNullOrEmpty(sysDictId) || sysDict==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        return ResultDTOBuilder.success(sysDict);
    }

    @RequestMapping("updateSysDict")
    public ResultDTO updateSysDict(@RequestBody SysDict sysDict)
    {
        if (sysDict==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        if (sysDictService.update(sysDict)!=null)
        {
            return ResultDTOBuilder.success("修改信息成功！");
        }
        return ResultDTOBuilder.failureMsg("修改信息失败！");
    }
    
    @RequestMapping("deleteSysDict")
    public ResultDTO deleteSysDict(@RequestBody Map<String,String> map)
    {
        String sysDictId = map.get("sysDictId");
        if (!Strings.isNullOrEmpty(sysDictId))
        {
            if (sysDictService.deleteById(Integer.parseInt(sysDictId)))
            {
                return ResultDTOBuilder.success("删除成功！");
            }
            return ResultDTOBuilder.failureMsg("删除失败！");
        }
        return ResultDTOBuilder.failureMsg("编号不正确！");
    }

    /**
     * 大分类列表
     * */
    @PostMapping("/mainCategory")
    public ResultDTO mainCategory(){
        List<SysDict> sysDictList = sysDictService.mainCategory();
        return ResultDTOBuilder.success(sysDictList);
    }
}
